﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Utilities;

namespace ManageTicket.Human
{
    public partial class frmPosition : frmBase
    {
        #region Contructor
        public frmPosition()
        {
            InitializeComponent();
        }
        #endregion

        #region Variable
        ManageTicket.Library.Control.ExecuteControl exeCtrl;

        //permission
        bool bAdd = false;
        bool bEdit = false;
        bool bDelete = false;
        bool bCancel = false;
        bool bSave = false;
        bool bFunction = false;
        bool bView = false;
        bool bFind = false;
        bool bExport = false;
        bool bSendmail = false;
        bool bPrint = false;

        clsMethod4All objMethod = new clsMethod4All();
        ManageTicket.Library.Common.CommonFunction clsCommonFunc = new ManageTicket.Library.Common.CommonFunction();       
        private Business.Position boPosition = new Business.Position();
        string sModel = ""; //Store AND or UPDATE status
        private const string ADD_MODEL = "ADD";
        private const string UPDATE_MODEL = "UPDATE";
        #endregion

        #region Event
        private void frmPosition_Load(object sender, EventArgs e)
        {
            //execute control
            exeCtrl = new ManageTicket.Library.Control.ExecuteControl();
            exeCtrl.Dock = DockStyle.Fill;
            exeCtrl.lblDescription.Text = "Cập nhật chức vụ";
            pnlExe.Controls.Add(exeCtrl);

            //permission
            clsCommonFunc.GetPermission(ref bAdd, ref bEdit, ref bDelete, ref bCancel, ref bSave, ref bFunction, ref bView, ref bFind, ref bExport, ref bSendmail, ref bPrint, frmMain.CurrentUser.UserId, "FORM_13");

            //set status
            LoadStatusOnce();

            View();
        }

        private void lvwPosition_SelectedIndexChanged(object sender, EventArgs e)
        {
            sModel = "";
            IsEdit();
            if (lvwPosition.SelectedItems.Count > 0)
            {
                ListViewItem item = lvwPosition.SelectedItems[0];

                PositionId.Text = item.Tag.ToString();
                txtPositionName.Text = item.Text;
                txtRemark.Text = item.SubItems[1].Text;

                LoadStatus(true, true, true, false, false, true);
            }
            else
            {
                LoadStatus(true, false, false, false, false, true);
            }
        }
        #endregion

        #region Execute
        public override void Add()
        {
            PositionId.Text = "";
            txtPositionName.Text = "";
            txtRemark.Text = "";

            LoadStatus(false, false, false, true, true, false);
            sModel = ADD_MODEL;
            IsEdit();
            txtPositionName.Focus();
        }

        public override void Edit()
        {
            if (PositionId.Text == "")
                return;
            LoadStatus(false, false, false, true, true, false);
            sModel = UPDATE_MODEL;
            IsEdit();
            txtPositionName.Focus();
        }

        public override void Delete()
        {
            try
            {
                if (PositionId.Text == "")
                    return;

                if (MessageBox.Show("Bạn có thực sự muốn xóa?", "Aquarius", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    //Check Existing in table
                    //if (this.CheckExitToDelete(oDept.DEPT_CODE)) {
                    //    MessageBox.Show(".", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    //    return;
                    //}

                    //delete
                    boPosition.DeleteByPK(PositionId.Text);

                    MessageBox.Show("Xóa phòng ban thành công.", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    View();
                } 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public override void Cancel()
        {
            PositionId.Text = "";
            txtPositionName.Text = "";
            txtRemark.Text = "";
            LoadStatus(false, false, false, false, false, true);
            View();
        }

        public override void Save()
        {
            if (CheckValid())
            {
                try
                {
                    if (sModel.Trim().ToUpper().Equals(ADD_MODEL))
                    {
                        AddPos();
                    }
                    else if (sModel.Trim().ToUpper().Equals(UPDATE_MODEL))
                    {
                        UpdatePos();
                    }
                    MessageBox.Show("Cập nhật chức vụ thành công.", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    View();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message + Environment.NewLine + "Không thể cập nhật chức vụ.", "Aquarius Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        public override void View()
        {
            try
            {
                lvwPosition.Items.Clear();

                ArrayList arrPos = new ArrayList();
                arrPos = boPosition.List();

                ListViewItem item;

                foreach (DataObjects.Position oPos in arrPos)
                {
                    item = new ListViewItem();
                    item.Tag = oPos.PositionId;
                    item.Text = oPos.PositionName;
                    item.SubItems.Add(oPos.Remark);

                    lvwPosition.Items.Add(item);
                }

                if (lvwPosition.Items.Count > 0)
                {
                    lvwPosition.Items[0].Selected = true;
                }
            }
            catch (Exception ex)
            {
                
                MessageBox.Show(ex.Message, "Aquarius Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion

        #region Method
        private void LoadStatusOnce()
        {
            exeCtrl.btnFunction.Enabled = false;
            exeCtrl.btnFind.Enabled = false;
            exeCtrl.btnExport.Enabled = false;
            exeCtrl.btnSendmail.Enabled = false;
            exeCtrl.btnPrint.Enabled = false;

            exeCtrl.btnFunction.BackColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnFind.ButtonColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnExport.ButtonColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnSendmail.ButtonColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnPrint.ButtonColor = Color.FromArgb(171, 171, 171);

            exeCtrl.btnFunction.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnFind.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnExport.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnSendmail.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnPrint.ForeColor = Color.FromArgb(241, 244, 251);
        }

        private void LoadStatus(bool blAdd, bool blEdit, bool blDelete, bool blCancel, bool blSave, bool blView)
        {
            exeCtrl.btnAdd.Enabled = blAdd & bAdd;
            exeCtrl.btnEdit.Enabled = blEdit & bEdit;
            exeCtrl.btnDelete.Enabled = blDelete & bDelete;
            exeCtrl.btnCancel.Enabled = blCancel & bCancel;
            exeCtrl.btnSave.Enabled = blSave & bSave;
            exeCtrl.btnView.Enabled = blView & bView;

            exeCtrl.btnAdd.ButtonColor = exeCtrl.btnAdd.Enabled ? Color.FromArgb(0, 163, 211) : Color.FromArgb(171, 171, 171);
            exeCtrl.btnEdit.ButtonColor = exeCtrl.btnEdit.Enabled ? Color.FromArgb(0, 163, 211) : Color.FromArgb(171, 171, 171);
            exeCtrl.btnDelete.ButtonColor = exeCtrl.btnDelete.Enabled ? Color.FromArgb(0, 163, 211) : Color.FromArgb(171, 171, 171);
            exeCtrl.btnCancel.ButtonColor = exeCtrl.btnCancel.Enabled ? Color.FromArgb(0, 163, 211) : Color.FromArgb(171, 171, 171);
            exeCtrl.btnSave.ButtonColor = exeCtrl.btnSave.Enabled ? Color.FromArgb(0, 163, 211) : Color.FromArgb(171, 171, 171);
            exeCtrl.btnView.ButtonColor = exeCtrl.btnView.Enabled ? Color.FromArgb(0, 163, 211) : Color.FromArgb(171, 171, 171);

            exeCtrl.btnAdd.ForeColor = exeCtrl.btnAdd.Enabled ? Color.FromArgb(0, 0, 0) : Color.FromArgb(241, 244, 251);
            exeCtrl.btnEdit.ForeColor = exeCtrl.btnEdit.Enabled ? Color.FromArgb(0, 0, 0) : Color.FromArgb(241, 244, 251);
            exeCtrl.btnDelete.ForeColor = exeCtrl.btnDelete.Enabled ? Color.FromArgb(0, 0, 0) : Color.FromArgb(241, 244, 251);
            exeCtrl.btnCancel.ForeColor = exeCtrl.btnCancel.Enabled ? Color.FromArgb(0, 0, 0) : Color.FromArgb(241, 244, 251);
            exeCtrl.btnSave.ForeColor = exeCtrl.btnSave.Enabled ? Color.FromArgb(0, 0, 0) : Color.FromArgb(241, 244, 251);
            exeCtrl.btnView.ForeColor = exeCtrl.btnView.Enabled ? Color.FromArgb(0, 0, 0) : Color.FromArgb(241, 244, 251);
        }

        private void IsEdit()
        {
            if (sModel == ADD_MODEL | sModel == UPDATE_MODEL)
            {
                txtPositionName.ReadOnly = false;
                txtRemark.ReadOnly = false;
            }
            else
            {
                txtPositionName.ReadOnly = true;
                txtRemark.ReadOnly = true;
            }
        }

        private bool CheckValid()
        {
            if (txtPositionName.Text.Trim().Length == 0)
            {
                MessageBox.Show("Vui lòng nhập tên chức vụ.", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtPositionName.Focus();
                return false;
            }
            return true;
        }

        private void AddPos()
        {
            //Add new 
            DataObjects.Position oPos = new DataObjects.Position();
            oPos.PositionId = (boPosition.GetMaxID() + 1).ToString();
            oPos.PositionName = txtPositionName.Text;
            oPos.Remark = txtRemark.Text;

            //Insert
            boPosition.Insert(oPos);        
        }

        private void UpdatePos()
        {
            //update
            DataObjects.Position oPos = new DataObjects.Position();
            oPos.PositionId = PositionId.Text;
            oPos.PositionName = txtPositionName.Text;
            oPos.Remark = txtRemark.Text;

            boPosition.Update(oPos);
        }
        #endregion
        
    }
}
